﻿using System;
using System.Collections.Generic;


namespace HAVNet.Core.Lambda
{
    public class Memo<ParamType, ReturnType>
    {
        Func        <ParamType, ReturnType> _function   = null;
        Dictionary  <ParamType, ReturnType> _dic        = new Dictionary<ParamType, ReturnType>();

        private Memo () { }

        public Memo (Func<ParamType, ReturnType> function)
        {
            _function = function;
        }

        public ReturnType execute (ParamType param)
        {
            if (! _dic.ContainsKey (param))
                _dic.Add (param, _function (param));

            return _dic [param];
        }

        public void reset ()
        {
            _dic.Clear ();
        }

        public void inspect (VFunc<Dictionary<ParamType, ReturnType>> inspectAction)
        {
            inspectAction (_dic);
        }
    }
}
